/*
 * Copyright (c) www.bugull.com
 */

package com.bugull.farm.device.server.handler;

import com.bugull.farm.core.cache.PropertiesCache;
import com.bugull.farm.core.frame.structure.Frame;
import com.bugull.farm.core.handler.AbstractDeviceFrameHandler;
import com.bugull.farm.core.handler.Processor;
import com.bugull.farm.device.server.processor.*;

import io.netty.channel.ChannelHandlerContext;

/**
 *
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class MyDeviceFrameHandler extends AbstractDeviceFrameHandler {
	
	private PropertiesCache props = PropertiesCache.getInstance();
	
    @Override
    protected void processFrame(ChannelHandlerContext ctx, Frame frame) throws Exception {
        byte cmd = frame.getByte("cmd");
        Processor processor = null;
        if(cmd == props.getByte("cmd.dev.join")){
        	processor = new DeviceJoinProcessor();
        }else if(cmd == props.getByte("cmd.dev.heartbeat")){
        	processor = new DeviceHeartbeatProcessor();
        }else if(cmd == props.getByte("cmd.dev.info")){
        	if(frame.getHexStr("serial").equals(props.get("serial"))){
        		processor = new DeviceInfoProcessor();
        	}else{
        		processor = new ControlProcessor();
        	}
        }else{
        	processor = new ControlProcessor();
        }
        processor.process(ctx, frame);
    }
}
